package com.wy.soap;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;

import com.wy.base.AbstractWizard;

public class SoapWizard extends AbstractWizard {

	public static String nameSpace = null;

	@Override
	public void addPages() {
		// page = new SoapPage(selection);
		// addPage(page);
		setPage(new SoapWizardPage(this.getSelection()));
		addPage(getPage());
	}

	@Override
	public boolean performFinish() {
		nameSpace = ((SoapWizardPage) getPage()).getNameSpaceName();
		return super.performFinish("/templates/wsendpoint.template");
	}

	@Override
	public void doFinish(String containerName, String packageName, String fileName, IProgressMonitor monitor,
			String templateUri) throws Exception {
		monitor.beginTask("Creating " + fileName, 2);
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
		IResource resource = root.findMember(new Path(containerName));
		if (!resource.exists() || !(resource instanceof IContainer)) {
			throwCoreException("Container \"" + containerName + "\" does not exist.");
		}
		IContainer container = (IContainer) resource;
		IJavaProject project = (IJavaProject) container.getProject().getNature("org.eclipse.jdt.core.javanature");
		IPackageFragment ipackage =
				project.getAllPackageFragmentRoots()[0].createPackageFragment(packageName, false, monitor);
		String contents = loadTemplate(containerName, templateUri);
		ipackage.createCompilationUnit(fileName + ".java", contents, false, monitor);
		String webserviceconfig = loadWebserviceconfig(containerName);
		ipackage.createCompilationUnit("WebServiceConfig.java", webserviceconfig, false, monitor);
		String saoprequest = loadSoapRequest(containerName);
		ipackage.createCompilationUnit("GetSoapRequest.java", saoprequest, false, monitor);
		String soapresponse = loadSoapResponse(containerName);
		ipackage.createCompilationUnit("GetSoapResponse.java", soapresponse, false, monitor);
		IFile file = container.getFile(new Path("/src/main/resources/sample.xsd"));
		InputStream stream = createFileAsStream(containerName);
		if (file.exists()) {
			file.setContents(stream, true, true, monitor);
		} else {
			file.create(stream, true, monitor);
		}
		stream.close();
		monitor.worked(1);
	}

	@Override
	public String loadTemplate(String containerName, String templateUri) throws Exception {
		StringBuffer sb = new StringBuffer();
		BufferedReader reader;
		InputStream input = this.getClass().getResourceAsStream(templateUri);
		reader = new BufferedReader(new InputStreamReader(input));
		String line = null;
		while ((line = reader.readLine()) != null) {
			System.out.println(line);
			line = line.replaceAll("#classname", fileName);
			line = line.replaceAll("#package", packageName);
			line = line.replaceAll("#namespace_uri", nameSpace);
			boolean skip = false;
			line = line.replaceAll("#projectname", containerName);
			if (!skip) {
				sb.append(line);
				sb.append("\n");
			}
		}
		reader.close();
		reader.close();
		return sb.toString();
	}

	private String loadWebserviceconfig(String containerName) throws Exception {
		StringBuffer sb = new StringBuffer();
		BufferedReader reader;
		InputStream input = this.getClass().getResourceAsStream("/templates/webserviceconfig.template");
		reader = new BufferedReader(new InputStreamReader(input));
		String line = null;
		while ((line = reader.readLine()) != null) {
			System.out.println(line);
			line = line.replaceAll("#classname", fileName);
			line = line.replaceAll("#package", packageName);
			line = line.replaceAll("#namespace_uri", nameSpace);
			boolean skip = false;
			line = line.replaceAll("#projectname", containerName);
			if (!skip) {
				sb.append(line);
				sb.append("\n");
			}
		}
		reader.close();
		reader.close();
		return sb.toString();
	}

	private String loadSoapRequest(String containerName) throws Exception {
		StringBuffer sb = new StringBuffer();
		BufferedReader reader;
		InputStream input = this.getClass().getResourceAsStream("/templates/getsoaprequest.template");
		reader = new BufferedReader(new InputStreamReader(input));
		String line = null;
		while ((line = reader.readLine()) != null) {
			System.out.println(line);
			line = line.replaceAll("#classname", fileName);
			line = line.replaceAll("#package", packageName);
			line = line.replaceAll("#namespace_uri", nameSpace);
			boolean skip = false;
			line = line.replaceAll("#projectname", containerName);
			if (!skip) {
				sb.append(line);
				sb.append("\n");
			}
		}
		reader.close();
		reader.close();
		return sb.toString();
	}

	private String loadSoapResponse(String containerName) throws Exception {
		StringBuffer sb = new StringBuffer();
		BufferedReader reader;
		InputStream input = this.getClass().getResourceAsStream("/templates/getsoapresponse.template");
		reader = new BufferedReader(new InputStreamReader(input));
		String line = null;
		while ((line = reader.readLine()) != null) {
			System.out.println(line);
			line = line.replaceAll("#classname", fileName);
			line = line.replaceAll("#package", packageName);
			line = line.replaceAll("#namespace_uri", nameSpace);
			boolean skip = false;
			line = line.replaceAll("#projectname", containerName);
			if (!skip) {
				sb.append(line);
				sb.append("\n");
			}
		}
		reader.close();
		reader.close();
		return sb.toString();
	}

	private String getSimpleXsd(String containerName) throws Exception {
		StringBuffer sb = new StringBuffer();
		BufferedReader reader;
		InputStream input = this.getClass().getResourceAsStream("/templates/sample-xsd.template");
		reader = new BufferedReader(new InputStreamReader(input));
		String line = null;
		while ((line = reader.readLine()) != null) {
			System.out.println(line);
			line = line.replaceAll("#classname", fileName);
			line = line.replaceAll("#package", packageName);
			line = line.replaceAll("#namespace_uri", nameSpace);
			boolean skip = false;
			line = line.replaceAll("#projectname", containerName);
			if (!skip) {
				sb.append(line);
				sb.append("\n");
			}
		}
		reader.close();
		reader.close();
		return sb.toString();
	}

	private InputStream createFileAsStream(String containerName) throws Exception {
		String contents = getSimpleXsd(containerName);
		return new ByteArrayInputStream(contents.getBytes());
	}
}